home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / utils / sound / players / os2 / sound242 / sound.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-31  |  2.4 KB  |  102 lines

  1. /*
  2. **    file            sound.c
  3. **    description    Sound sample player for OS/2
  4. **    author        Paul van Keep
  5. **    copyright    (NC)Not Copyrighted by the Frobozz Magic Software Company
  6. **    remarks        Plays .snd & mac sound files
  7. **                    compile with MSC 6.0
  8. **
  9. ** use and distribute freely
  10. ** 
  11. */
  12. #include <stdio.h>
  13. #include <malloc.h>
  14. #include <stdlib.h>
  15. #include <fcntl.h>
  16. #include <io.h>
  17.  
  18. #define INCL_DOS
  19. #include <os2.h>
  20.  
  21. extern void pascal play(char far *buf, int size, char freq);
  22.  
  23. #define NRBUFS        20
  24. static char *block[NRBUFS];
  25. static unsigned short bsize[NRBUFS];
  26.  
  27. #define BS    (unsigned short)(1024 * 60)
  28.  
  29. static short data[4];
  30. static short freq[] = { 22000, 11000, 7333, 5500 , 4400, 3666, 3143, 2750, 2444, 2200, 2000 };
  31.  
  32. unsigned char hdr[128];
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36.     register int i, j, k;
  37.     unsigned short s;
  38.     long len, base;
  39.     FILE *f;
  40.     if (argc == 1) {
  41.         printf("Usage: sound <file> ... \n");
  42.         return 1;
  43.     }
  44.     DosPortAccess(0, 0, 0x21, 0x21);                // IRQ mask
  45.     DosPortAccess(0, 0, 0x42, 0x43);                // Timer 2 access
  46.     DosPortAccess(0, 0, 0x61, 0x61);                // speaker output
  47.     for (k = 1; k < argc; k++) {
  48.         f = fopen(argv[k], "rb");
  49.         if (f == NULL) {
  50.             printf("Cannot open file %s\n", argv[k]);
  51.             continue;
  52.         }
  53.         printf("Playing %s\r", argv[k]);
  54.         fread(hdr, sizeof(hdr), 1, f);
  55.         if (hdr[1] == 0 || (hdr[1] == 0xFF)) {                // it is an SND file
  56.             memcpy(data, hdr, 4 * sizeof(short));
  57.             base = 8L;
  58.         } else {
  59.             data[0] = 0;
  60.             data[1] = 22000;
  61.             data[2] = 10;
  62.             data[3] = 4;
  63.             base = 0L;
  64.         }
  65.         if (memcmp(hdr + 65, "FSS", 3) == 0) {        // has header
  66.             len = 256L * 256L * 256L * hdr[83] +
  67.                              256L * 256L * hdr[84] +
  68.                                      256L * hdr[85] +
  69.                                                hdr[86];
  70.             base = 128L;
  71.         } else {
  72.             len = filelength(fileno(f)) - base;
  73.         }
  74.         fseek(f, base, 0);
  75.         for (i = 0; i < sizeof(freq)/sizeof(short); i++) {
  76.             if (data[1] >= freq[i]) {
  77.                 data[1] = i + 1;
  78.                 break;
  79.             }
  80.         }
  81.         i = 0;
  82.         while (len > 0L) {
  83.             bsize[i] = (len > (unsigned long)BS) ? BS : (unsigned short)len;
  84.             len -= (long)bsize[i];
  85.             block[i] = malloc(bsize[i]);
  86.             fread(block[i], 1, bsize[i], f);
  87.             i++;
  88.         }
  89.         fclose(f);
  90.         for (j = 0; j < i; j++) {
  91.             play(block[j], bsize[j], (char)data[1]);
  92.         }
  93.         for (j = 0; j < i; j++) {
  94.             free(block[j]);
  95.         }
  96.         DosPortAccess(0, 1, 0x61, 0x61);                // speaker output
  97.         DosPortAccess(0, 1, 0x42, 0x43);                // Timer 2 access
  98.         DosPortAccess(0, 1, 0x21, 0x21);                // IRQ mask
  99.     }
  100.     return 0;
  101. }
  102.